/**
 * @a https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/description/
 */



#include "common.h"



//Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        if(!root) return {};
        vector<vector<int>> ret;
        queue<TreeNode*> que;
        que.push(root);
        int bin_flag = 0;
        while(que.size())
        {
            int sz = que.size();
            vector<int> tmp;
            while(sz--){
                TreeNode* curNode = que.front();
                que.pop();
                tmp.push_back(curNode->val);
                if(curNode->left)
                    que.push(curNode->left);
                if(curNode->right)
                    que.push(curNode->right);
            }
            if(bin_flag == 1){
                bin_flag = 0;
                reverse(tmp.begin(),tmp.end());
            }
            else if(bin_flag == 0)
                bin_flag = 1;
            ret.push_back(tmp);
        }
        return ret;
    }
};